package com.junjie.checkin.controller;


import com.junjie.checkin.response.*;
import com.junjie.checkin.response.admin.BusinessDataResponse;
import com.junjie.checkin.response.admin.RecentActivitiesResponse;
import com.junjie.checkin.response.front.UserFrontResponse;
import com.junjie.checkin.service.AdminActivityService;
import com.junjie.checkin.service.DashboardService;
import com.junjie.checkin.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


@RestController
@RequestMapping("/admin")
public class DashboardController {
    @Autowired
    private DashboardService dashboardService;

    @Autowired
    private AdminActivityService adminActivityService;

    @Autowired
    private UserService userService;


    /**
     * 获取今日活动数据
     * @return
     */
    @GetMapping("/dashboard/stats")
    public Result<BusinessDataResponse> getTodayActivity() {
        //获取当天开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获取当天结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);
        BusinessDataResponse todayActivity = dashboardService.getTodayActivity(begin,end);
        return Result.getResult(ResultStatus.OK,todayActivity);
    }

    /**
     * 获取最近活动数据
     * @return
     */
    @GetMapping("/activity/list")
    public Result<PageResult<RecentActivitiesResponse>> getRecentActivities() {
        int pageSize = 10; // 固定取前4条
        List<RecentActivitiesResponse> activities = adminActivityService.getLatestActivities(pageSize);

        PageResult<RecentActivitiesResponse> pageResult = new PageResult<>();
        pageResult.setRecords(activities);
        pageResult.setTotal(activities.size());

        return Result.getResult(ResultStatus.OK,pageResult);
    }

    /**
     * 获取排行榜
     * @return
     */
    @GetMapping("/dashboard/leaderboard")
    public Result<PageResult<UserFrontResponse>> getRecentUsers() {
        int pageSize = 7; // 固定取前7条
        List<UserFrontResponse> Users = userService.getLatestUsers(pageSize);

        PageResult<UserFrontResponse> pageResult = new PageResult<>();
        pageResult.setRecords(Users);
        pageResult.setTotal(Users.size());

        return Result.getResult(ResultStatus.OK,pageResult);
    }
}
